package com.yuanfengerp.basicInfoManage.logisticscompany.service.impl;

import com.yuanfengerp.basicInfoManage.logisticscompany.dao.ILogisticsCompanyInfoMapper;
import com.yuanfengerp.basicInfoManage.logisticscompany.moudel.LogisticsCompanyInfo;
import com.yuanfengerp.basicInfoManage.logisticscompany.service.ILogisticsCompanyService;
import com.yuanfengerp.publicUtil.Result;
//import io.lettuce.core.dynamic.annotation.Param;
import org.apache.ibatis.annotations.Param;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * 物流公司信息服务实现类
 */
@Service
public class LogisticsCompanyServiceImpl implements ILogisticsCompanyService {
    @Autowired
    private ILogisticsCompanyInfoMapper ilcim;
    Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 删除物流公司信息
     *
     * @param logistics_companies_ID 物流公司ID
     * @return 删除结果
     */
    @Override
    public Result<String> deleteLogisticsCompanyInfo(Integer logistics_companies_ID) {
        if (logistics_companies_ID == null || logistics_companies_ID <= 0) {
            logger.warn("尝试删除无效的物流公司ID: {}", logistics_companies_ID);
            return Result.failure(400, "无效的物流公司ID");
        }
        try {
            int i = ilcim.deleteLogisticsCompanyInfo(logistics_companies_ID);
            logger.info("删除物流信息成功: ID={}", logistics_companies_ID);
            return i > 0 ? Result.success("删除成功") : Result.failure(400, "删除失败");
        } catch (Exception e) {
            logger.error("删除物流信息时发生异常: ID={}", logistics_companies_ID, e);
            return Result.failure(500, "系统错误");
        }
    }

    /**
     * 插入物流公司信息
     *
     * @param logisticsCompanyInfo 物流公司信息对象
     * @return 插入结果
     */
    @Override
    public Result<String> insertLogisticsCompanyInfo(LogisticsCompanyInfo logisticsCompanyInfo) {
        if (logisticsCompanyInfo == null) {
            logger.warn("尝试添加无效的物流信息: {}", logisticsCompanyInfo);
            return Result.failure(400, "无效的物流信息");
        }
        try {
            logisticsCompanyInfo.setLogistics_companies_state(0);
            logisticsCompanyInfo.setLogistics_companies_operator("admin");
            logisticsCompanyInfo.setLogistics_companies_operate_time(new Timestamp(System.currentTimeMillis()));
            logisticsCompanyInfo.setLogistics_companies_montifction(new Timestamp(System.currentTimeMillis()));
            int i = ilcim.insertLogisticsCompanyInfo(logisticsCompanyInfo);
            logger.info("添加物流信息成功: {}", logisticsCompanyInfo);
            return i > 0 ? Result.success("添加成功") : Result.failure(400, "添加失败");
        } catch (Exception e) {
            logger.error("添加物流信息时发生异常: {}", logisticsCompanyInfo, e);
            return Result.failure(500, "系统错误");
        }
    }

    /**
     * 查询所有物流公司信息
     *
     * @return 公司信息列表
     */
    @Override
    public Result<List<LogisticsCompanyInfo>> selectAllLogisticsCompanyInfo(@Param("pageSize") Integer pageSize, @Param("pageNum") Integer pageNum) {
        Logger logger = LoggerFactory.getLogger(this.getClass());

        try {
            if (ilcim == null) {
                logger.error("ipmim is null");
                return Result.failure(500, "内部错误");
            }
            int totalRecords = ilcim.getTotalCount();
//            Integer pageNum = (Integer) params.get("pageNum");
//            Integer pageSize = (Integer) params.get("pageSize");
            // 获取总记录数，这里假设传入了合适的查询参数（如param1和param2）
            if (pageNum == null)
                pageNum = 5;
            if (pageSize == null)
                pageSize = 1;
            // 计算总页数
            int totalPages = (int) Math.ceil((double) totalRecords / pageNum);

            // 判断页码是否超出范围
            if (pageSize > totalPages)
                return Result.failure(400, "页码超出有效范围");
            pageSize = (pageSize - 1) * pageNum;
            List<LogisticsCompanyInfo> logisticsCompanyInfos = ilcim.selectAllLogisticsCompanyInfo(pageSize, pageNum);

            if (logisticsCompanyInfos != null) {
                logger.info("Successfully retrieved pay method info list");
                return Result.success(logisticsCompanyInfos);
            } else {
                logger.warn("No data found");
                return Result.failure(404, "没有数据");
            }
        } catch (Exception e) {
            logger.error("Error occurred while selecting all pay method info", e);
            return Result.failure(500, "内部错误");
        }
    }

    /**
     * 根据条件查询物流公司信息
     *
     * @param @Param("logistics_companies_ID") int logistics_companies_ID,
     * @return 公司信息列表
     * @Param("logistics_companies_name") String logistics_companies_name,
     * @Param("logistics_companies_state") int logistics_companies_state 查询参数
     */
    @Override
    public Result<List<LogisticsCompanyInfo>> selectLogisticsCompanyInfo(@Param("logistics_companies_ID") Integer logistics_companies_ID,
                                                                         @Param("logistics_companies_name") String logistics_companies_name,
                                                                         @Param("logistics_companies_state") Integer logistics_companies_state) {
        List<LogisticsCompanyInfo> logisticsCompanyInfos = ilcim.selectLogisticsCompanyInfo(logistics_companies_ID, logistics_companies_name, logistics_companies_state);
        if (logisticsCompanyInfos.isEmpty())
            return Result.failure(400, "没有数据");
        return Result.success(logisticsCompanyInfos);
    }

    /**
     * 更新物流公司信息
     *
     * @param logisticsCompanyInfo 更新后的物流公司信息对象
     * @return 更新结果
     */
    @Override
    public Result<String> updateLogisticsCompanyInfo(LogisticsCompanyInfo logisticsCompanyInfo) {
        if (logisticsCompanyInfo == null) {
            logger.warn("尝试修改无效的物流信息: {}", logisticsCompanyInfo);
            return Result.failure(400, "无效的物流信息");
        }
        try {
            logisticsCompanyInfo.setLogistics_companies_montifction(new Timestamp(System.currentTimeMillis()));
            int i = ilcim.updateLogisticsCompanyInfo(logisticsCompanyInfo);
            logger.info("修改物流信息成功: {}", logisticsCompanyInfo);
            return i > 0 ? Result.success("修改成功") : Result.failure(400, "修改失败");
        } catch (Exception e) {
            logger.error("修改物流信息时发生异常: {}", logisticsCompanyInfo, e);
            return Result.failure(500, "系统错误");
        }
    }
}
